<?php

	require_once(dirname(__FILE__) . "/../core/safeParam.php");
	
	$pid	= $_GET['pid'];
	
	if (!is_numeric($pid)){
		error_log("Producto $pid inválido");
  		echo "Disculpe pero se ha producido un error, en instantes lo estaremos solucionando";
  		exit();
	}
	
	$main_item = $database->select("SELECT * FROM menu_item WHERE id = '$pid'");
	
	if (!$main_item){
		error_log(mysql_error());
  		exit();
	}
	
	$query3 = $database->query("SELECT * FROM menu_item WHERE id = '$pid'");
	
	while ($list3 = mysql_fetch_array($query3)) {
  		$itemid          = $list3['id'];
  		$itemname        = $list3['name'];
  		$itemmenu        = $list3['menu'];
  		$itemdescription = $list3['description'];
  		$itemprice       = unserialize($list3['price']);
  		$itemprice       = str_replace("$", "", $itemprice);
  		$itemfeature     = $list3['feature'];
  		$itemcat         = $list3['cat'];
  		$itemimage       = $list3['image'];
  		$options         = unserialize($list3['options']);
	}

	$query3 = $database->query("SELECT * FROM menu_cat WHERE id = '$itemcat'");
	
	while ($list3 = mysql_fetch_array($query3)) {
  		$cat_price = unserialize($list3['price']);
	} 

	if (isset($_GET['price']) && in_array($_GET['price'], $itemprice)) {
  		$current_price = $_GET['price'];
	} else {
  		$current_price = $itemprice[0];
	}

	$prices_options = "";
	
	if (count($cat_price) > 0) {
		for ($i = 0; $i < count($cat_price); $i++) {
			if (isset($cat_price[$i]['name']) && $cat_price[$i]['name'] != '' && isset($itemprice[$i]) && $itemprice[$i] != '') {
				// get prices
      		if (isset($itemprice[$i]) && $current_price == $itemprice[$i]) {
        			$selected = 'selected="selected"';
      		} else {
        			$selected = "";
      		}
      		
      		$prices_options .= ' <option value="' 
        							. $cat_price[$i] . '|' 
        							. $itemprice[$i] . '" ' 
        							. $selected . '>' 
        							. $cat_price[$i] . ': ' 
        							. price_format($itemprice[$i]) . '</option> ';
    		}
  		}
	}
	
	$results_opt = $database->select("SELECT * FROM options_set WHERE pid = '$pid' or global = 1 and rid = " . RID . " order by pos");
	
	if ($results_opt != FALSE) {
		for ($j = 0; $j < count($results_opt); $j++) {
			$results_opt[$j]['options'] = $database->select("SELECT * FROM options_items WHERE parent = " . $results_opt[$j]['id'] . " order by name");
  		} //$j = 0; $j < count($results_opt); $j++
	} //$results_opt != FALSE

	$additional_images = getOption("additional_image_$pid", RID, false, true);
	
	$smarty->assign('additional_images', $additional_images);
	$smarty->assign('newoptions', $results_opt);
	$smarty->assign('current_price', price_format($current_price));
	$smarty->assign('current_price_nf', $current_price);
	$smarty->assign('prices', $prices_options);
	$smarty->assign('cat_price', $cat_price);
	$smarty->assign('item_desc', nl2br($itemdescription));
	$smarty->assign('item_name', $itemname);
	$smarty->assign('item_cat', $itemcat);
	$smarty->assign('item_id', $itemid);
	$smarty->assign('item_price', $itemprice);
	$smarty->assign('item_image', $itemimage);
	$smarty->assign('main_item', $main_item[0]);
	$smarty->assign('7base', getOption('7-inch-base', RID));
	$smarty->assign('7baseStart', explode("|", getOption('7-inch-base', RID)));
	$smarty->assign('9base', getOption('9-inch-base', RID));
	$smarty->assign('12base', getOption('12-inch-base', RID));
	$smarty->assign('16base', getOption('16-inch-base', RID));

	$mainprice = "";
	
	if (isset($_GET['price'])){
  		$mainprice = $_GET['price'];
}

$smarty->assign('mainprice', $mainprice);

?>
